package net.chenyan.server.controller.admapi;/**
 * @author chenyan
 * @createTime 2022/5/8 21:11
 * @description description
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.chenyan.commons.core.R;
import net.chenyan.server.po.*;
import net.chenyan.server.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 *@description: TODO
 *@author: chenyan
 *@create: 2022/5/8 21:11
 */
@Slf4j
@Api(tags = {"用户Mv接口"})
@RestController
@RequestMapping(value = "/admapi/userMv")
public class UserMvController {
    @Autowired
    private UserMvService userMvService;
    @Autowired
    private UserService userService;
    @Autowired
    private MvService mvService;

    @ApiOperation(value = "用户点赞/取消点赞Mv")
    @PostMapping(value = "/addLikedNum")
    public R addLikedNum(@RequestBody int userId, int mvId) {
        User user=userService.getById(userId);
        Mv mv=mvService.getById(mvId);
        if(user==null || mv==null){
            return R.error("用户id或者mvid不存在！");
        }
        int likeNum=mv.getLikedNum();
        QueryWrapper<UserMv> userMvQueryWrapper=new QueryWrapper<>();
        userMvQueryWrapper.eq("user_id",userId);
        userMvQueryWrapper.eq("mv_id",mvId);
        UserMv userMv=userMvService.getOne(userMvQueryWrapper);
        if(userMv==null){
            UserMv userMv1=new UserMv();
            userMv1.setMvId(mvId);
            userMv1.setUserId(userId);
            userMv1.setIsLike(1);
            userMvService.save(userMv1);
            mv.setLikedNum(likeNum+1);
            mvService.updateById(mv);
            return R.ok("点赞成功");
        }else{
            if(userMv.getIsLike()==1){
                userMv.setIsLike(0);
                mv.setLikedNum(likeNum-1);
                mvService.updateById(mv);
                userMvService.updateById(userMv);
                return R.ok("取消点赞成功");
            }else{
                userMv.setIsLike(1);
                mv.setLikedNum(likeNum+1);
                mvService.updateById(mv);
                userMvService.updateById(userMv);
                return R.ok("点赞成功");
            }

        }

    }

    @ApiOperation(value = "用户收藏/取消收藏mv")
    @PostMapping(value = "/addCollect")
    public R addCollect(@RequestBody int userId,int mvId) {
        User user=userService.getById(userId);
        Mv mv=mvService.getById(mvId);
        if(user==null || mv==null){
            return R.error("用户id或者mvid不存在！");
        }
        int collectNum=mv.getCollectedNum();
        QueryWrapper<UserMv> userMvQueryWrapper=new QueryWrapper<>();
        userMvQueryWrapper.eq("user_id",userId);
        userMvQueryWrapper.eq("mv_id",mvId);
        UserMv userMv=userMvService.getOne(userMvQueryWrapper);
        if(userMv==null){
            UserMv userMv1=new UserMv();
            userMv1.setMvId(mvId);
            userMv1.setUserId(userId);
            userMv1.setIsCollect(1);
            userMvService.save(userMv1);
            mv.setCollectedNum(collectNum+1);
            mvService.updateById(mv);
            return R.ok("收藏成功");
        }else{
            if(userMv.getIsCollect()==1){
                userMv.setIsCollect(0);
                mv.setCollectedNum(collectNum-1);
                mvService.updateById(mv);
                userMvService.updateById(userMv);
                return R.ok("取消收藏成功");
            }else{
                userMv.setIsCollect(1);
                mv.setCollectedNum(collectNum+1);
                mvService.updateById(mv);
                userMvService.updateById(userMv);
                return R.ok("收藏成功");
            }
        }

    }

    @ApiOperation(value = "用户评论歌曲")
    @PostMapping(value = "/addComment")
    public R addComment(@RequestBody int userId,int mvId,String comment) {
        User user=userService.getById(userId);
        Mv mv=mvService.getById(mvId);
        if(user==null || mv==null){
            return R.error("用户id或者mvid不存在！");
        }
        QueryWrapper<UserMv> userMvQueryWrapper=new QueryWrapper<>();
        userMvQueryWrapper.eq("user_id",userId);
        userMvQueryWrapper.eq("mv_id",mvId);
        UserMv userMv=userMvService.getOne(userMvQueryWrapper);
        if(userMv==null){
            UserMv userMv1=new UserMv();
            userMv1.setMvId(mvId);
            userMv1.setUserId(userId);
            userMv1.setComment(comment);
            userMvService.save(userMv1);
            return R.ok("评论成功");
        }else{
            userMv.setComment(comment);
            boolean temp = userMvService.updateById(userMv);
            return temp ? R.ok("修改成功") : R.error("修改失败");
        }
    }

    @ApiOperation(value = "获取用户收藏的mv")
    @PostMapping(value = "/getMvByUserId")
    public R getMvByUserId(int userId, @RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size) {
        return R.data(mvService.findAllMvByUserId(PageRequest.of(page,size),userId));
    }
}
